<?php
include_once dirname(__FILE__) . '/profile.class.php';
 
class Seller extends Main
{
	public $_user_id;
	public $_name;
	public $_cname;
	public $_username;
	public $_sesso;
	public $_altezza;
	public $_peso;
	public $_seno;
	public $_mailer;
	public $_email;
	public $_img1;
	public $_img2;
	public $_img3;
	public $_img4;
	public $_telefono1;
	public $_dt_nascita;
	public $_capelli;
	public $_occhi;
	public $_citta;
	public $_tipo_via;
	public $_indirizzo;
	public $_civico;
	public $_cap;
	public $_nazionalita;
	public $_res;
	public $_gms_tipo;
	public $_f_attivo;
	public $_f_attivo_admin;
	public $_IP;
	public $_f_reg_admin;
	
		public function ReadProfilesListData($page_rows)
		{			
			$sql = "SELECT utenti.user_id, tipo_utente,f_attivo,f_attivo_admin, f_confermato, nome,cognome,username,mail,prefisso1,prefisso2,telefono1,telefono2, dt_nascita,publicdate,regdate, id_citta,tipo_via,indirizzo,civico,cap, id_nazionalita,sesso,altezza,peso,seno,capelli,occhi,annuncio,zona,IP,f_reg_admin,dt_invio_benvenuto,dt_invio_attivazione,dt_invio_sollecito,ultimi_accessi.dt_accesso ";
            $sql .= "FROM utenti LEFT JOIN (SELECT user_id, max(dt_accesso) dt_accesso FROM `utenti_accessi` group by user_id) as ultimi_accessi ON utenti.user_id = ultimi_accessi.user_id WHERE tipo_utente =".USER_USER." AND id_seller =$_SESSION[user_id] ";
				
			if(!isset($_GET['order']))
				$sql .= "ORDER BY regdate DESC, nome, username";
			else {
				$order = $_GET['order'];	
				if(!isset($_GET['direz']))			
					$sql .= "ORDER BY ".$order." ASC, nome, username";
				else
					$sql .= "ORDER BY ".$order." ".$_GET['direz'].", nome, username";
			}

			$contatore = $this->CountProfiles(USER_ESCORT, $_SESSION['user_id']);
			$n_row = @mysql_fetch_array($contatore) or die ("Errore nell'applicazione.");
			
			$pagine_totali = ceil($n_row[0]/$page_rows);
			$pag_visualizzata = (!isset($_GET['npage'])) ? 1 : (int)$_GET['npage'];
			$show = ($pag_visualizzata - 1) * $page_rows;				
		
			$sql= $sql." LIMIT $show, $page_rows";
		
			$this->_res = mysql_query ($sql, $this->conn);
			//die($sql);
 			include '../template/' . TEMPLATE_ADMIN_NAME . '/profiles.php';
			return;
		}
			
		public function CountProfiles($tipo_utente, $id_seller)
		{
			$sql = "SELECT count(utenti.user_id) FROM utenti ";
			$sql .= "WHERE tipo_utente = ".$tipo_utente." AND id_seller = ".$id_seller;
			$result = mysql_query ($sql, $this->conn);
			return $result;
		}

		public function ReadAgencyListData($page_rows)
		{			
			$sql = "SELECT utenti.user_id, tipo_utente,num_annunci_allow, f_attivo,f_attivo_admin, denominazione,username,mail,prefisso1,prefisso2,telefono1,telefono2, publicdate,regdate, id_citta,tipo_via,indirizzo,civico,cap,zona,IP,f_reg_admin,dt_invio_benvenuto,dt_invio_attivazione,dt_invio_sollecito,ultimi_accessi.dt_accesso ";
            $sql .= "FROM utenti LEFT JOIN (SELECT user_id, max(dt_accesso) dt_accesso FROM `utenti_accessi` group by user_id) as ultimi_accessi ON utenti.user_id = ultimi_accessi.user_id WHERE tipo_utente=".USER_ESCORT." AND id_seller =$_SESSION[user_id] ";
				
			if(!isset($_GET['order']))
				$sql .= "ORDER BY regdate DESC, denominazione, username";
			else {
				$order = $_GET['order'];	
				if(!isset($_GET['direz']))			
					$sql .= "ORDER BY ".$order." ASC, denominazione, username";
				else
					$sql .= "ORDER BY ".$order." ".$_GET['direz'].", denominazione, username";
			}
			
			$contatore = $this->CountProfiles(USER_AGENCY);
			$n_row = @mysql_fetch_array($contatore) or die ("Errore nell'applicazione.");
			
			$pagine_totali = ceil($n_row[0]/$page_rows);
			$pag_visualizzata = (!isset($_GET['npage'])) ? 1 : (int)$_GET['npage'];
			$show = ($pag_visualizzata - 1) * $page_rows;				
		
			$sql= $sql." LIMIT $show, $page_rows";
		
			$this->_res = mysql_query ($sql, $this->conn);
			//die($sql);
 			include '../template/' . TEMPLATE_ADMIN_NAME . '/agencies.php';
			return;
		}

		public function ReadProfilesCity()
		{
			$sql = "SELECT citta.id_citta, citta.nome as nome_citta, count(user_id) as num_annunci from utenti left join citta on utenti.id_citta = citta.id_citta ";
			$sql .= "WHERE tipo_utente = '3' AND f_attivo='S' AND utenti.f_attivo_admin = 'S' AND publicdate != '' AND id_seller =$_SESSION[user_id]";
            $sql .= "GROUP BY citta.id_citta, nome_citta ";
			if(!isset($_GET['order']))
				$sql .= "ORDER BY num_annunci DESC, nome_citta";
			else {
				$order = $_GET['order'];
				if(!isset($_GET['direz']))
					$sql .= "ORDER BY ".$order." ASC";
				else
					$sql .= "ORDER BY ".$order." ".$_GET['direz'];
			}

			$this->_res = mysql_query ($sql, $this->conn);
			//die($sql);
 			include '../template/' . TEMPLATE_ADMIN_NAME . '/profiles_city.php';
			return;
		}
		public function ReadProfilesClickClassification()
		{
			//$sql = "SELECT user_id, tipo_utente,f_attivo, utenti.nome,cognome,username,mail,prefisso1,prefisso2,telefono1,telefono2, dt_nascita, utenti.id_citta,citta.nome as citta, id_nazionalita,sesso,mail,num_click_tot,num_click_sett,num_click_mese,total_value,IFNULL(total_votes,0) as total_votes FROM utenti LEFT JOIN citta ON utenti.id_citta = citta.id_citta LEFT JOIN ratings ON utenti.user_id = ratings.id WHERE tipo_utente = '3' AND f_attivo='S' AND utenti.f_attivo_admin = 'S' AND publicdate != '' ";
			$sql = "SELECT user_id, tipo_utente,f_attivo, utenti.nome,cognome,username,mail,prefisso1,prefisso2,telefono1,telefono2, dt_nascita, utenti.id_citta,citta.nome as citta, id_nazionalita,sesso,mail,visite_tot.num_click_tot,num_click_sett,visite.num_click_mese,total_value,IFNULL(total_votes,0) as total_votes FROM utenti LEFT JOIN citta ON utenti.id_citta = citta.id_citta LEFT JOIN ratings ON utenti.user_id = ratings.id LEFT JOIN
			(SELECT count(id_utente) as num_click_mese, id_utente FROM (
SELECT DISTINCT id_utente, IP, date(dt_visita) as dt_format FROM visite WHERE DATE_FORMAT(dt_visita, '%Y')=DATE_FORMAT(CURDATE(),'%Y') AND DATE_FORMAT(dt_visita, '%m')=DATE_FORMAT(CURDATE(),'%m')) visite_uniche
group by id_utente ) visite ON utenti.user_id = visite.id_utente LEFT JOIN
			(SELECT count(id_utente) as num_click_tot, id_utente FROM (
SELECT DISTINCT id_utente, IP, date(dt_visita) as dt_format FROM visite) visite_tot_uniche
group by id_utente ) visite_tot ON utenti.user_id = visite_tot.id_utente
WHERE tipo_utente = '3' AND f_attivo='S' AND utenti.f_attivo_admin = 'S' AND publicdate != '' AND id_seller =$_SESSION[user_id] ";
			if(isset($_GET['sesso']) && strlen($_GET['sesso'])>0)
			{
				$sql .= "AND sesso='".$_GET['sesso']."' ";
				$this->_sesso=$_GET['sesso'];
			}	
			
			if(!isset($_GET['gms_tipo']))
				$this->_gms_tipo = "G";
			else
				$this->_gms_tipo = $_GET['gms_tipo'];
				
			switch ($this->_gms_tipo) {
			case "G":
				$sql .= "ORDER BY num_click_tot DESC, publicdate, regdate";
				break;
			case "S":
				$sql .= "ORDER BY num_click_sett DESC, publicdate, regdate";
				break;
			case "M":
				$sql .= "ORDER BY num_click_mese DESC, publicdate, regdate";
				break;   
			case "V":
				$sql .= "ORDER BY total_value/total_votes DESC, total_votes, publicdate, regdate";
				break;   
			case "P":
				$sql .= "ORDER BY num_click_mese+((total_value/total_votes)*total_votes) DESC, total_votes, publicdate, regdate";
				break;   
			default:
				$sql .= "ORDER BY num_click_tot-total_votes DESC, publicdate, regdate";
			}
							
			$this->_res = mysql_query ($sql, $this->conn);
			//die($sql);
 			include '../template/' . TEMPLATE_ADMIN_NAME . '/classification.php';
			return;
		}
		public function NumProfilesActives($sesso)
		{
			$sql = "SELECT COUNT(user_id) as num_utenti_attivi FROM utenti WHERE tipo_utente = '3' AND f_attivo='S' AND f_attivo_admin = 'S' AND publicdate != '' AND id_seller =$_SESSION[user_id]";
			
			if(isset($sesso) && $sesso!="")
				$sql .= " AND sesso = '".$sesso."'";
				
			$result = mysql_query ($sql, $this->conn);		
			$row = mysql_fetch_array($result);
			
			$num_utenti_attivi = $row['num_utenti_attivi'];
			mysql_free_result($result);
			return $num_utenti_attivi;
		}
		
		public function NumProfilesActivesNoPhoto()
		{
			$sql = "SELECT COUNT(user_id) as num_utenti FROM utenti WHERE tipo_utente = '3' AND f_attivo='S' AND f_attivo_admin = 'S' AND publicdate = '' AND id_seller =$_SESSION[user_id]";
			
			$result = mysql_query ($sql, $this->conn);		
			$row = mysql_fetch_array($result);
			
			$num_utenti = $row['num_utenti'];
			mysql_free_result($result);
			return $num_utenti;
		}
		
		public function NumProfilesNoConfirmed()
		{
			$sql = "SELECT COUNT(user_id) as num_utenti FROM utenti WHERE tipo_utente = '3' AND f_attivo='N' AND f_confermato = 'N' AND id_seller =$_SESSION[user_id]";
			
			$result = mysql_query ($sql, $this->conn);		
			$row = mysql_fetch_array($result);
			
			$num_utenti = $row['num_utenti'];
			mysql_free_result($result);
			return $num_utenti;
		}
		public function CompositeContent()
		{
			$page = basename($_SERVER['PHP_SELF']);
 
			switch($page)
			{
				case "profiles.php":				
				$this->ReadProfilesListData(50);
				break;
				
				case "classification.php":				
				$this->ReadProfilesClickClassification();
				break;

                case "profiles_city.php":
				$this->ReadProfilesCity();
				break;
			}
		}
}
 
?>
